package dongze.com.ckdj.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import dongze.com.ckdj.R;

/**
 * Created by renxiaoxiao on 2017/8/12.
 * 根据子view个数进行排列
 */

public class MassLossRadioGroup extends RadioGroup {
    private static final String TAG = "MassLossRadioGroup";

    private int CELL_NUM = 4;
    private int rows = 0;
    private int radiobuttonWidth = 0;
    private int radioButtonHeight = 0;
    private Boolean childerClickable = true;


    public MassLossRadioGroup(Context context) {
        super(context);
    }

    public MassLossRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MassLossRadioGroupStyle);
        childerClickable = typedArray.getBoolean(R.styleable.MassLossRadioGroupStyle_childrenClickable, true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() > 0) {
            RadioButton button = (RadioButton) getChildAt(0);
            radiobuttonWidth = button.getMeasuredWidth();
            radioButtonHeight = button.getMeasuredHeight();
            if (CELL_NUM > getChildCount()) {
                setMeasuredDimension(button.getMeasuredWidth() * getChildCount(), button.getMeasuredHeight());
            } else {
                if (getChildCount() % CELL_NUM == 0) {
                    rows = getChildCount() / CELL_NUM;
                } else {
                    rows = getChildCount() / CELL_NUM + 1;
                }
                setMeasuredDimension(button.getMeasuredWidth() * CELL_NUM, button.getMeasuredHeight() * rows);
            }
        }
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (getChildCount() > 0) {
            if (CELL_NUM > getChildCount()) {
                for (int i = 0; i < getChildCount(); i++) {
                    int left = radiobuttonWidth * i;
                    int top = 0;
                    RadioButton radioButton = (RadioButton) getChildAt(i);
                    radioButton.layout(left, top, left + radiobuttonWidth, top + radioButtonHeight);
                    radioButton.setClickable(childerClickable);
                }
            } else {
                if (getChildCount() % CELL_NUM == 0) {
                    rows = getChildCount() / CELL_NUM;
                } else {
                    rows = getChildCount() / CELL_NUM + 1;
                }
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < CELL_NUM; j++) {
                        if (i * CELL_NUM + j < getChildCount()) {
                            int left = radiobuttonWidth * j;
                            int top = radioButtonHeight * i;
                            RadioButton radioButton = (RadioButton) getChildAt(i * CELL_NUM + j);
                            radioButton.layout(left, top, left + radiobuttonWidth, top + radioButtonHeight);
                            radioButton.setClickable(childerClickable);

                        }
                    }
                }
            }
        }
    }
}
